package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Shop;
import org.csu.mall.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/shop/")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping("check_Shop_is_open")
    @ResponseBody
    public UniformResponse<String> checkShopIsOpen(){
        UniformResponse<String> response = shopService.checkShopIsOpen();
        return response;
    }


    @PostMapping("open_shop")
    @ResponseBody
    public UniformResponse<Shop> openShop(Shop shop, HttpSession session){
        UniformResponse<Shop> response = shopService.openShop(shop);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHOP,response.getData());
        }
        return response;
    }
    @GetMapping("search_shop")
    @ResponseBody
    UniformResponse<Shop> searchShop(HttpSession session){
        UniformResponse<Shop> response = shopService.searchShop();
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHOP,response.getData());
        }
        return response;
    }
    @PostMapping("update_shop")
    @ResponseBody
    UniformResponse<Shop> updateShop(Shop shop, HttpSession session){
        UniformResponse<Shop> response = shopService.updateShop(shop);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHOP,response.getData());
        }
        return response;
    }
    @PostMapping("close_shop")
    @ResponseBody
    UniformResponse<String> closeShop(String id){
        UniformResponse<String> response = shopService.closeShop(id);
        return response;
    }
}
